
package G25part;

import java.util.ArrayList;
import java.util.List;
import java.net.*;
import java.io.*;
import java.util.regex.*;

/**
 *
 * @author xgx
 */
public class ProxiesLeecher{
    
    private List<String> Proxies;
    private List<String> URLS;
    private String strona;
    private String u;
    //Define proxy pattern
    private static String PROXY = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d{1,5})";  
    
    public ProxiesLeecher(String u)
    {
        Proxies=new ArrayList<String>();
        URLS=new ArrayList<String>();
        this.u = u;
    }
    
    //Return proxy
    public List<String> showProxies()
    {
        return Proxies;
    }
    
    public String pipe(InputStream in,String charset) throws IOException {
        StringBuilder s = new StringBuilder();
        if(charset==null||"".equals(charset)){
            charset="utf-8";
        }
        String rLine = null;
        BufferedReader bReader = new BufferedReader(new InputStreamReader(in,charset));
        while ( (rLine = bReader.readLine()) != null) {
            String tmp_rLine = rLine;
            int str_len = tmp_rLine.length();
            if (str_len > 0) {
              s.append(tmp_rLine);
            }
            tmp_rLine = null;
        }
        in.close();
        return s.toString();
    }
    
    //Download page and get proxy
    public void getProxies()
    {
        try
        {
        Pattern pattern=Pattern.compile(PROXY);
        URL www = new URL(u);
        HttpURLConnection httpCon = (HttpURLConnection) www.openConnection();

        strona = pipe(httpCon.getInputStream(),"ISO-8859-1");
        
        Matcher matcher = pattern.matcher(strona);
        while (matcher.find()) {
            Proxies.add(matcher.group());
        }

        }
        catch (Exception ex)
        {
            //Write to error file all errors...
        }
    }
    
    public void proxyChecker(String proxy)
    {
        
    }
    
    
}

